package com.boot.security.server.dict.service.impl;

import com.boot.security.server.common.Result;
import com.boot.security.server.common.ResultBuilder;
import com.boot.security.server.dict.mapper.DataDictDetailsMapper;
import com.boot.security.server.dict.model.DataDictDetails;
import com.boot.security.server.dict.model.LayUiModel;
import com.boot.security.server.dict.param.DictDetailsParam;
import com.boot.security.server.dict.service.IDictDetailsService;
import com.boot.security.server.dict.vo.DictDetailsVo;
import com.boot.security.server.utils.BeanHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 字典详情业务实现
 * @author Administrator
 */
@SuppressWarnings("rawtypes")
@Service
@Slf4j
public class DictDetailsServiceImpl implements IDictDetailsService {

    @Autowired
    private DataDictDetailsMapper detailsMapper;

    @Override
    public LayUiModel findByDictId (DictDetailsParam param, HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int index = (page - 1) * limit;
        int count = detailsMapper.count(param);
        List<DictDetailsVo> vos = new ArrayList<>();
        if (count > 0) {
            List<DataDictDetails> details = detailsMapper.findByDictId(param.getDictId(), index, limit);
            details.forEach(dictDetails -> {
                DictDetailsVo vo = BeanHelper.copyProperties(dictDetails, DictDetailsVo.class);
                vos.add(vo);
            });
            return new LayUiModel(count, vos);
        }
        return new LayUiModel(0, vos);
    }

    @Override
    public LayUiModel findById (Integer id) {
        DataDictDetails dict = detailsMapper.findById(id);
        List<DictDetailsVo> vos = new ArrayList<>();
        DictDetailsVo vo = BeanHelper.copyProperties(dict, DictDetailsVo.class);
        vos.add(vo);
        return new LayUiModel(1, vos);
    }

    @Override
    public Result add (DataDictDetails details) {
        int dictId = details.getDictId();
        int key = details.getDataKey();
        int k = detailsMapper.findByDataKeyAndDictId(key, dictId);
        if (k > 0) {
            return ResultBuilder.systemError("该数据的key（编号）已存在！");
        }
        int n = detailsMapper.findByDataValueAndDictId(details.getDataValue(), dictId);
        if (n > 0) {
            return ResultBuilder.systemError("该数据的值已存在！");
        }
        List<DataDictDetails> detailsList = detailsMapper.queryByDictId(dictId);
        if (detailsList != null && detailsList.size() > 0) {
            int sort = detailsList.get(0).getSort() + 1;
            details.setSort(sort);
        } else {
            details.setSort(1);
        }
        int i = detailsMapper.add(details);
        if (i > 0) {
            return ResultBuilder.success();
        }
        log.info("新增数据字典");
        return ResultBuilder.systemError("新增数据字典失败！");
    }

    @Override
    public Result update (DataDictDetails details) {
        int dictId = details.getDictId();
        int key = details.getDataKey();
        String value = details.getDataValue();
        int k = detailsMapper.findByDataKeyAndDictIdAndId(key, dictId, details.getId());
        if (k > 0) {
            return ResultBuilder.systemError("该数据的key（编号）已存在！");
        }
        int n = detailsMapper.findByDataValueAndDictIdAndId(value, dictId, details.getId());
        if (n > 0) {
            return ResultBuilder.systemError("该数据的值已存在！");
        }
        DataDictDetails dict = detailsMapper.findById(dictId);
        dict.setDataKey(key);
        dict.setDataValue(value);
        int i = detailsMapper.update(dict);
        if (i > 0) {
            return ResultBuilder.success();
        }
        log.info("修改数据字典");
        return ResultBuilder.systemError("修改数据字典失败！");
    }

    @Override
    public Result delete (Integer id) {
        int i = detailsMapper.delete(id);
        if (i > 0) {
            return ResultBuilder.success();
        }
        log.info("删除数据字典");
        return ResultBuilder.systemError("删除数据字典失败！");
    }

    @Override
    public LayUiModel findMaxDataKeyByDictId (Integer dictId) {
        Integer key = detailsMapper.findMaxDataKeyByDictId(dictId);
        List<Integer> keys = new ArrayList<>();
        if (key != null) {
            keys.add(key + 1);
        } else {
            keys.add(1);
        }
        return new LayUiModel(1, keys);
    }
}
